package com.sunnada.demo.log.mongodb;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.log4j.spi.LoggingEvent;
import org.log4mongo.LoggingEventBsonifierImpl;

import java.util.Date;

/**
 * @program: mybatisproject2
 * @description: log4mongo文档类型定义
 * @author: 李瑞益
 * @create: 2018-10-28 14:26
 **/
public class ConciseLoggingEventBsonifier extends LoggingEventBsonifierImpl {

    @Override
    public DBObject bsonify(LoggingEvent loggingEvent) {
        BasicDBObject result = null;
        if (loggingEvent != null) {
            result = new BasicDBObject();
            result.put("timestamp" , loggingEvent.getTimeStamp()+8*3600*1000);
            this.nullSafePut(result , "level" , loggingEvent.getLevel().toString());
            this.nullSafePut(result , "thread" , loggingEvent.getThreadName());
            this.nullSafePut(result , "message" , loggingEvent.getMessage());
            this.nullSafePut(result , "loggerName" , loggingEvent.getLoggerName());
            this.nullSafePut(result , "userName",loggingEvent.getMDC("userName"));
            this.nullSafePut(result , "userId" , loggingEvent.getMDC("userId"));
            this.nullSafePut(result , "url" , loggingEvent.getMDC("url"));
            this.nullSafePut(result , "date" , new Date(loggingEvent.getTimeStamp()+8*3600*1000));
            this.addHostnameInformation(result);
            // this.addMDCInformation(result, loggingEvent.getProperties());
            // this.addLocationInformation(result,
            // loggingEvent.getLocationInformation());
            // this.addThrowableInformation(result,
            // loggingEvent.getThrowableInformation());
        }
        return result;
    }
}